package com.test.demo.transaction;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.io.UnsupportedEncodingException;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Gao
 */
public class TransactionProducer {

    public static void main(String[] args) throws MQClientException,
                                            UnsupportedEncodingException, InterruptedException {
        TransactionMQProducer transactionMQProducer = new TransactionMQProducer("tx_producer");
        transactionMQProducer.setNamesrvAddr("81.70.58.152:9876");
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        transactionMQProducer.setExecutorService(executorService);
        //本地事务的监听
        transactionMQProducer.setTransactionListener(new TransactionListenerLocal());
        transactionMQProducer.start();

        for (int i = 0; i < 10; i++) {
            String orderId = UUID.randomUUID().toString();
            String body = "{'operation':'doOrder','orderId':'" + orderId + "'}";
            Message message = new Message("order_tx_topic",
                    "TagA", orderId, body.getBytes(RemotingHelper.DEFAULT_CHARSET));
            transactionMQProducer.sendMessageInTransaction(message, orderId + "&" + i);
            Thread.sleep(1000);
        }
    }
}
